package com.gombe.credit.app.common

import android.content.Context
import android.view.Gravity
import android.view.WindowManager
import com.gombe.credit.app.R
import com.gombe.credit.app.GombeCreditApp
import com.gombe.credit.app.base.BaseDialog
import com.gombe.credit.app.event.GombeCreditEventPointHelper
import com.gombe.credit.app.request.HttpHelper
import com.gombe.credit.app.request.IHttpCallback
import com.gombe.credit.app.utils.GombeCreditDensityUtil
import org.json.JSONObject

/**
 * CommonDialog
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:32
 * @Annotation：
 */
abstract class CommonDialog(context: Context): BaseDialog(context) {

    override fun initGombeCreditWindow() {
        val params = window?.attributes
        params?.width = GombeCreditApp.app.getPhoneWidth() - GombeCreditDensityUtil.dp2px(32f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    /**
     * post
     */
    fun doPostAction(httpUrl: String, params: JSONObject, callback: IHttpCallback){
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                callback.onRequestSuccess(data)
            }

            override fun onRequestError(error: JSONObject) {
                callback.onRequestError(error)
                showGombeCreditToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                callback.onRequestFinish()
            }
        })
    }

    /**
     * 添加埋点
     */
    fun addEventPointRequestAction(eventName: String, eventRemark: String = "") {
        GombeCreditEventPointHelper.addEventPointRequestAction(eventName,eventRemark)
    }

    /**
     * getString
     */
    fun getString(strId: Int): String{
        return GombeCreditApp.app.getString(strId)
    }

    /**
     * getColor
     */
    fun getColor(color: Int): Int{
        return GombeCreditApp.app.getColor(color)
    }

    override fun show() {
        super.show()
        HttpHelper.setTag(this)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.cancelTag(this)
    }
}